package cn.etynit.order.service.impl;

import cn.etynit.basic.config.BussinessException;
import cn.etynit.order.domain.OrderPetAcquisition;
import cn.etynit.order.dto.OrderConfirmDto;
import cn.etynit.order.mapper.OrderPetAcquisitionMapper;
import cn.etynit.order.service.IOrderPetAcquisitionService;
import cn.etynit.basic.service.impl.BaseServiceImpl;
import cn.etynit.pet.domain.Pet;
import cn.etynit.pet.domain.SearchMasterMsg;
import cn.etynit.pet.service.IPetService;
import cn.etynit.pet.service.ISearchMasterMsgService;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etynit
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IPetService petService;



    /**
     * 确认订单
     * @param orderConfirmDto
     */
    @Override
    @Transactional
    public void confirm(OrderConfirmDto orderConfirmDto) throws MQClientException {
        // 根据Id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderConfirmDto.getId());
        // 不存在抛异常
        if (orderPetAcquisition == null) {
            throw new BussinessException("订单不存在！");
        }
        // 查询寻主信息，修改状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        // 寻主消息不存在抛异常
        if (searchMasterMsg == null) {
            throw new BussinessException("寻主消息不存在！");
        }
        // 状态设置为3，已完成
        searchMasterMsg.setState(3);
        // 更新寻主信息
        searchMasterMsgService.update(searchMasterMsg);
        // 保存宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        // 添加金额信息，注意转String类型
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        petService.add(pet);
        // 修改订单状态为完成
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);

    }


    /**
     * 取消订单
     * @param orderId
     */
    @Override
    @Transactional
    public void cancel(Long orderId) {
        // 根据Id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(orderId);
        // 查询不到订单抛异常
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 修改订单状态为-1（取消订单）
        orderPetAcquisition.setState(-1);
        // 更新订单信息
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 根据该订单找到寻主消息
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findById(orderPetAcquisition.getSearchMasterMsgId());
        // 如果消息不存在，抛异常
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        // 设置订单状态为1（待处理）
        searchMasterMsg.setState(1);
        // 清空店铺id，返回寻主池
        searchMasterMsg.setShopId(null);
        // 更新订单信息
        searchMasterMsgService.update(searchMasterMsg);

        // 4.@TODO 保存取消日志，不确定是否有表，没有自己设计
    }

    /**
     * 根据订单号查询订单
     * @param message
     * @return
     */
    @Override
    public OrderPetAcquisition findByOrderSn(Long message) {

        return orderPetAcquisitionMapper.findByOrderSn(message);
    }
}
